import sys
import socket
import struct
import os
import random
import time
import threading

loop = {}
lock = threading.Lock()
def capture(s,i):
    global loop
    try:
        while(loop[i]):
            sd = (b'\xAA\x00\xFF\x55\x00\x00\x00\x01\x31\xAA\x00\xFF\x55\x00\x00\x00\x0A\x50\x00\x00\x01\x00\x00\x01\x00\x00\x03\xAA\x00\xFF\x55\x00\x00\x00\x0A\x50\x00\x00\x01\x00'+
                b'\x00\x01\x00\x00\x03\xAA\x00\xFF\x55\x00\x00\x00\x0A\x50\x00\x00\x01\x00\x00\x01\x00\x00\x03\xAA\x00\xFF\x55\x00\x00\x00\x0A\x50\x00\x00\x01\x00\x00\x01\x00\x00\x03\xAA\x00\xFF'+
                b'\x55\x00\x00\x00\x0A\x50\x00\x00\x01\x00\x00\x01\x00\x00\x03')
            s.send(sd)
            print("send({}):{}".format(i,sd.hex()))
            time.sleep(0.01)
    except Exception as e:
        print(e)

def scoket(i):
    global loop
    while True:
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect(('localhost',3654))
            while True:
                recv_data = s.recv(1024)
                print("recv({}):{}".format(i,bytes(recv_data[:9]).hex(),bytes(recv_data[9:]).hex()))
                if len(recv_data)>8:
                    if recv_data[8]==0x10:
                        if(i==0):
                            sd = (bytearray([0xaa,0x00,0xff,0x55,0x00,0x00,0x00,0x2C,0x10])+
                                b'\x57\x4C\x4E\x2D\x54\x57\x2D\x56\x31\x30\x30\x2C\x35\x32\x30\x31\x2D\x31\x30\x30\x2D\x56\x30\x30\x30\x30\x31\x2C\x46\x2C\x30\x31\x2E\x32\x33\x34\x2C\x32\x2E\x34\x47\x48\x7A')
                        elif(i==1):
                            sd = (bytearray([0xaa,0x00,0xff,0x55,0x00,0x00,0x00,0x2E,0x10])+
                                b'\x57\x4C\x4E\x2D\x54\x57\x2D\x52\x54\x44\x31\x30\x30\x2C\x35\x32\x30\x37\x2D\x31\x30\x30\x2D\x52\x30\x30\x30\x30\x31\x2C\x46\x2C\x30\x31\x2E\x32\x33\x34\x2C\x32\x2E\x34\x47\x48\x7A')
                        elif(i==2):
                            sd = (bytearray([0xaa,0x00,0xff,0x55,0x00,0x00,0x00,0x2D,0x10])+
                                b'\x57\x4C\x4E\x2D\x54\x57\x2D\x54\x43\x31\x30\x30\x2C\x35\x32\x30\x38\x2D\x31\x30\x30\x2D\x54\x30\x30\x30\x30\x31\x2C\x46\x2C\x30\x31\x2E\x32\x33\x34\x2C\x32\x2E\x34\x47\x48\x7A')
                        elif(i==3):
                            sd = (bytearray([0xaa,0x00,0xff,0x55,0x00,0x00,0x00,0x2D,0x10])+
                                b'\x57\x4C\x4E\x2D\x54\x57\x2D\x53\x47\x31\x30\x30\x2C\x35\x32\x30\x35\x2D\x31\x30\x30\x2D\x53\x30\x30\x30\x30\x31\x2C\x46\x2C\x30\x31\x2E\x32\x33\x34\x2C\x32\x2E\x34\x47\x48\x7A')
                        elif(i==4):
                            sd = (bytearray([0xaa,0x00,0xff,0x55,0x00,0x00,0x00,0x2F,0x10])+
                                b'\x57\x4C\x4E\x2D\x54\x57\x2D\x49\x45\x50\x45\x31\x30\x30\x2C\x35\x32\x30\x34\x2D\x31\x30\x30\x2D\x45\x30\x30\x30\x30\x31\x2C\x46\x2C\x30\x31\x2E\x32\x33\x34\x2C\x32\x2E\x34\x47\x48\x7A')
                        elif(i==5):
                            sd = (bytearray([0xaa,0x00,0xff,0x55,0x00,0x00,0x00,0x2D,0x10])+
                                b'\x57\x4C\x4E\x2D\x54\x57\x2D\x55\x41\x31\x30\x30\x2C\x35\x32\x30\x34\x2D\x31\x30\x30\x2D\x55\x30\x30\x30\x30\x31\x2C\x46\x2C\x30\x31\x2E\x32\x33\x34\x2C\x32\x2E\x34\x47\x48\x7A')
                        s.send(sd)
                        print("send({}):{}".format(i,sd.hex()))
                    elif recv_data[8]==0x30:
                        with lock:loop[i] = True
                        threading.Thread(target=capture,args=(s,i)).start()
                    elif recv_data[8]==0x20:
                        with lock:loop[i] = False
                        sd = bytes([0xaa,0x00,0xff,0x55,0x00,0x00,0x00,0x01,recv_data[8]+1])
                        s.send(sd)
                        print("send({}):{}".format(i,sd.hex()))
                    else:
                        sd = bytes([0xaa,0x00,0xff,0x55,0x00,0x00,0x00,0x01,recv_data[8]+1 if recv_data[8]<0x60 else recv_data[8]])
                        s.send(sd)
                        print("send({}):{}".format(i,sd.hex()))
                elif len(recv_data)==0:
                    break
        except socket.error as e:
            print(e)

if __name__=="__main__":
    for i in range(5):
        loop[i]=True
        threading.Thread(target=scoket,args=(i,)).start()
    input('input to exit')